<?php
namespace Todo\Controller;
use Todo\Builder\FormBuilder;
use Todo\Builder\ListBuilder;
use Todo\Service\ExportService;
use Todo\Service\LoginService;
use Todo\Service\LogService;
use Think\Page;

/**
 * Class BackendController
 * 后台类应用基类控制器
 * @package Todo\Controller
 */
class BackendController extends BaseController {

    protected $_user = null;
    protected $_model = null;
    protected $_order = 'id DESC';
    protected $_limit = 20;

    protected function _initialize(){
        parent::_initialize();

        $this->_user = LoginService::getInstance()->getLoginUser(true);
        define('UID', $this->_user['id']);
        
        $appInfo = D('Todo/Sys/Application')->keyFind(C('APP_ID'));
        $this->assign('_app', $appInfo);
    }

    /**
     * 列表
     */
    public function index(){
        $this->display('index');
    }

    /**
     * 添加
     */
    public function add(){
        $this->display();
    }

    /**
     * 获取要编辑的数据，建议重写
     * @param $id
     * @return mixed
     */
    protected function _editData($id){
        $this->_loadModel();
        return $this->_model->keyFind($id);
    }

    /**
     * 编辑
     */
    public function edit(){
        $id = I('get.id');
        if(!$id) $this->error('非法请求!');
        $data = $this->_editData($id);
        $this->assign('vo', $data);
        $this->display();
    }

    /**
     * 导入
     */
    public function import(){
        $tmplFile = strtolower(CONTROLLER_NAME.'_import.xls');
        $this->assign('tmplFile',$tmplFile);
        $this->display('Common@Public/import');
    }

    /**
     * 加载模型类，需要重写
     */
    protected function _loadModel(){
        if(!$this->_model) $this->_model = D('Todo/'.MODULE_NAME.'/'.CONTROLLER_NAME);
    }
}